package org.jeecg.modules.monitoring.j_monitoring.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.monitoring.j_monitoring.entity.EndpointVO;
import org.jeecg.modules.monitoring.j_monitoring.entity.MacEndPoint;
import org.jeecg.modules.monitoring.j_monitoring.utils.HuaweiOpt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/**
* @Description: 设备终端
* @Author: xiaoyq
* @Date:   2023-03-09
* @Version: V1.0
*/
@Api(tags="设备终端")
@RestController
@RequestMapping("/j_monitoring/endPoint")
@Slf4j
public class EndPointController {

    @Autowired
    HuaweiOpt hwOpt;
   /**
    * 分页列表查询
    *
    * @param endPoint
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "设备终端-分页列表查询")
   @RequiresPermissions("设备终端:查询")
   @ApiOperation(value="设备终端-分页列表查询", notes="设备终端-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(MacEndPoint endPoint,
                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                         HttpServletRequest req) {
       Boolean isLogin = hwOpt.checkLogin();
       if (isLogin) {
           JSONObject result = hwOpt.getPageList(pageNo, pageSize, endPoint.getNode(), endPoint.getMac(),endPoint.getDeviceType(),endPoint.getIp());
           result.putIfAbsent("current",pageNo);
           result.putIfAbsent("size",pageSize);
           Boolean success = result.getBoolean("success");
           if(success){
               result.putIfAbsent("records",result.get("data"));
               result.remove("data");
               return Result.OK(result);
           }
       }
       return Result.error("查询异常.");
   }

   /**
    *   添加
    *
    * @return
    */
   @AutoLog(value = "设备终端-添加")
   @ApiOperation(value="设备终端-添加", notes="设备终端-添加")
   @RequiresPermissions("设备终端:添加")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody EndpointVO endPoint) {
       Boolean isLogin = hwOpt.checkLogin();
       Boolean isOk = false;
       if (isLogin) {
           isOk = hwOpt.addEndpoint(endPoint);
       }
       if(isOk){
           return Result.OK("添加成功！");
       }
       else{
           return Result.error("添加失败！");
       }
   }

   /**
    *  编辑
    *
    * @param endPoint
    * @return
    */
   @AutoLog(value = "设备终端-编辑")
   @ApiOperation(value="设备终端-编辑", notes="设备终端-编辑")
   @RequiresPermissions("设备终端:编辑")
   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
   public Result<String> edit(@RequestBody EndpointVO endPoint) {
       Boolean isLogin = hwOpt.checkLogin();
       Boolean isOk = false;
       if (isLogin) {
           isOk = hwOpt.updateEndpoint(endPoint);
       }
       if(isOk){
           return Result.OK("编辑成功！");
       }
       else{
           return Result.error("编辑失败！");
       }
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "设备终端-通过id删除")
   @ApiOperation(value="设备终端-通过id删除", notes="设备终端-通过id删除")
   @RequiresPermissions("设备终端:删除")
   @DeleteMapping(value = "/delete")
   public Result<String> delete(@RequestParam(name="id",required=true) String id,
                                @RequestParam(name="groupId",required=true) Integer groupId,
                                @RequestParam(name="mac",required=true) String mac) {
       Boolean isLogin = hwOpt.checkLogin();
       Boolean isOk = false;
       if (isLogin) {
           isOk = hwOpt.batchDeleteEndpoint(id,groupId,mac);
       }
       if(isOk){
           return Result.OK("删除成功！");
       }
       else{
           return Result.error("删除失败！");
       }
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "设备终端-批量删除")
   @ApiOperation(value="设备终端-批量删除", notes="设备终端-批量删除")
   @RequiresPermissions("设备终端:批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids,
                                     @RequestParam(name="groupId",required=true) Integer groupId,
                                     @RequestParam(name="macs",required=true) String macs) {
       Boolean isLogin = hwOpt.checkLogin();
       Boolean isOk = false;
       if (isLogin) {
           isOk = hwOpt.batchDeleteEndpoint(ids,groupId,macs);
       }
       if(isOk){
           return Result.OK("批量删除成功！");
       }
       else{
           return Result.error("批量删除失败！");
       }
   }

   /**
    * 通过id查询
    *
    * @param mac
    * @return
    */
   @RequiresPermissions("设备终端:查询")
   @ApiOperation(value="设备终端-通过id查询", notes="设备终端-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="mac",required=true) String mac,
                                        @RequestParam(name="node",required=true) String node) {
       Boolean isLogin = hwOpt.checkLogin();
       JSONObject result = null;
       if (isLogin) {
           result = hwOpt.getOne(node,mac);
       }
       if(null==result){
           return Result.error("查询失败！");
       }
       return Result.OK(result);
   }
}
